package gw.com.android.presenter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.Stack;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.ui.BaseActivity;
import www.com.library.app.AppActivities;
import www.com.library.app.Logger;
import www.com.library.model.DataItemDetail;

/**
 * 新手福利小广告
 * 游客在APP停留5分钟后，弹出
 *
 * @author jett
 * @since 2018-09-07.
 */
public class NoviceWelfarePresenter {

    private final String TAG = "novice_welfare";
    private static final NoviceWelfarePresenter instance = new NoviceWelfarePresenter();

    private boolean mainActivityStarted = false; // MainActivity 是否已经启动
    private boolean adsCompleted = false;       //  广告是否已经成功加载
    private Runnable mTask;
    private AdsData.PicData adData;
    private boolean cancel = false;
    private boolean showed = false;

    private NoviceWelfarePresenter() {
    }

    public static NoviceWelfarePresenter getInstance() {
        return instance;
    }

    public void onMainActivityStarted() {
        mainActivityStarted = true;
        start();
    }

    public void onAdsLoadCompleted() {
        adsCompleted = true;
        start();
    }

    private void start() {
        if (!mainActivityStarted || !adsCompleted) {
            // 只有当MainActivity 成功启动，并且广告成功加载后，才开始计时
            return;
        }
        AppMain.getApp().getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                realStart();
            }
        }, 1000);
    }

    private void realStart() {
        Logger.i(TAG, "start");
        if (GTConfig.instance().getAccountType() != GTConfig.ACC_TYPE_GUEST) {
            return;
        }
        AdsData ad = AdsConfigPresenter.getInstance().getAd(AdsConfigPresenter.TYPE_NOVICE_WELFARE);
        if (ad == null || ad.adPics == null || ad.adPics.isEmpty()) {
            Logger.i(TAG, "ad is empty");
            return;
        }
        adData = ad.adPics.get(0);
        if (adData == null) {
            Logger.i(TAG, "ad is empty");
            return;
        }
        mTask = new Runnable() {
            @Override
            public void run() {
                showAll(adData.title, adData.link);
            }
        };
        AppMain.getApp().getHandler().postDelayed(mTask, 1000 * 1 * 5);
        Logger.i(TAG, "ad prepared.");
    }

    public void cancel() {
        cancel = true;
        if (mTask != null) {
            AppMain.getApp().getHandler().removeCallbacks(mTask);
            mTask = null;
        }
        Logger.i(TAG, "cancel..");
    }

    public boolean needShow() {
        return showed && !cancel;
    }

    public AdsData.PicData getAdData() {
        return adData;
    }

    private void showAll(String title, String link) {
        if (GTConfig.instance().getAccountType() != GTConfig.ACC_TYPE_GUEST) {
            return;
        }
        if (cancel) {
            Logger.i(TAG, "ad is already canceled");
            return;
        }
        showed = true;
        Stack<Activity> activities = AppActivities.getSingleton().getAllActivities();
        if (activities != null && !activities.empty()) {
            for (Activity activity : activities) {
                if (activity != null && activity instanceof BaseActivity) {
                    ((BaseActivity) activity).showNoviceWelfare(title, link);
                }
            }
        }
        Logger.i(TAG, "ad showed..");
    }

    public void hideAll() {
        showed = false;
        Stack<Activity> activities = AppActivities.getSingleton().getAllActivities();
        if (activities != null && !activities.empty()) {
            for (Activity activity : activities) {
                if (activity != null && activity instanceof BaseActivity) {
                    ((BaseActivity) activity).hideNoviceWelfare();
                }
            }
        }
        Logger.i(TAG, "ad hiden..");
    }

    public View createView(final Activity activity, final String title, final String link) {
        View rootViw = View.inflate(activity, R.layout.view_novice_welfare, null);
        ImageView ivIcon = rootViw.findViewById(R.id.novice_welfare_icon);
        TextView tvContent = rootViw.findViewById(R.id.novice_welfare_text);
        tvContent.setText(title);
        rootViw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataItemDetail detail = new DataItemDetail();
                detail.setStringValue(ConfigType.CONFIG_TYPE_TYPE_URL_TAG, link);
                detail.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, title);
                ActivityManager.showWebPageActivity(activity, detail, null);
                NoviceWelfarePresenter.getInstance().hideAll();
            }
        });

        AnimatorSet set = new AnimatorSet();
        ObjectAnimator rotate = ObjectAnimator.ofFloat(ivIcon, "rotation", 0, 18, -18, 18, -18, 18, -18, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
        rotate.setDuration(3000);
        rotate.setRepeatCount(ValueAnimator.INFINITE);
        rotate.setInterpolator(new LinearInterpolator());

        ObjectAnimator scaleX = ObjectAnimator.ofFloat(ivIcon, "scaleX", 1, 1.3F, 1.3F, 1.3F, 1.3F, 1.3F, 1.3F, 1.3F, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
        scaleX.setDuration(3000);
        scaleX.setRepeatCount(ValueAnimator.INFINITE);
        scaleX.setInterpolator(new LinearInterpolator());

        ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivIcon, "scaleY", 1, 1.3F, 1.3F, 1.3F, 1.3F, 1.3F, 1.3F, 1.3F, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
        scaleY.setDuration(3000);
        scaleY.setRepeatCount(ValueAnimator.INFINITE);
        scaleY.setInterpolator(new LinearInterpolator());

        set.playTogether(rotate, scaleX, scaleY);
        set.start();
        rootViw.setTag(set);
        return rootViw;
    }

}
